package com.flying.rsocket;

import com.flying.rsocket.model.MarketData;
import com.flying.rsocket.model.MarketDataRequest;
import org.reactivestreams.Publisher;
import org.springframework.messaging.rsocket.RSocketRequester;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MarketDataRestController {
    //    private final Mono<RSocketRequester> rSocketRequester;
//
//    public MarketDataRestController(RSocketRequester.Builder rsocketRequesterBuilder) {
//        this.rSocketRequester = rsocketRequesterBuilder.connectTcp("localhost", 7000).cache();
//    }
//
//    @GetMapping("/current/{stock}")
//    public Publisher<MarketData> current(@PathVariable("stock") String stock) {
//        return this.rSocketRequester.flatMap(req ->
//                req.route("currentMarketData")
//                        .data(new MarketDataRequest(stock))
//                        .retrieveMono(MarketData.class));
//    }
    private final RSocketRequester rSocketRequester;

    public MarketDataRestController(RSocketRequester rSocketRequester) {
        this.rSocketRequester = rSocketRequester;
    }

    @GetMapping("/current/{stock}")
    public Publisher<MarketData> current(@PathVariable("stock") String stock) {
        return this.rSocketRequester.route("currentMarketData")
                .data(new MarketDataRequest(stock))
                .retrieveMono(MarketData.class)
                .log();
    }
}
